<?php

/**
 * login actions.
 *
 * @package    samsung
 * @subpackage login
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class loginActions extends sfActions {

  /**
   * Executes index action
   *
   * @param sfRequest $request A request object
   */
  public function executeIndex(sfWebRequest $request) {

    if ($this->getUser()->isAuthenticated()) {
      $this->redirect('@home');
    }

    $this->form = new SignInForm(array(Constants::USER_GROUP_ADMIN));
    if ($request->isMethod('post')) {
      $formData = $request->getParameter(SignInForm::FORM_NAME);
      $this->form->bind($formData);

      if ($this->form->isValid()) {
          $user = $this->form->getUser();
          $this->getUser()->setAuthenticated(true);
          $this->getUser()->setAttribute(Constants::SESSION_USER_ID, $user->getId());
          $this->getUser()->setAttribute(Constants::SESSION_USERNAME, $user->getUsername());
          $this->getUser()->setAttribute(Constants::SESSION_FIRST_NAME, $user->getFirstName());
          $this->getUser()->setAttribute(Constants::SESSION_LAST_NAME, $user->getLastName());
          $this->getUser()->setAttribute(Constants::SESSION_USER_GROUP_ID, $user->getUserGroupId());
          $this->getUser()->addCredentials($user->getUSER_GROUP()->getGroupName());
          $this->getUser()->addCredentials($user->getUSER_GROUP()->getId());
          $this->redirect('@home');
      }
    }
  }

  public function executeLogout(sfWebRequest $request) {
    $this->getUser()->setAuthenticated(false);
    $this->getUser()->getAttributeHolder()->remove(Constants::SESSION_USER_ID);
    $this->getUser()->getAttributeHolder()->remove(Constants::SESSION_USERNAME);
    $this->getUser()->getAttributeHolder()->remove(Constants::SESSION_FIRST_NAME);
    $this->getUser()->getAttributeHolder()->remove(Constants::SESSION_LAST_NAME);
    $this->getUser()->getAttributeHolder()->remove(Constants::SESSION_USER_GROUP_ID);
    $this->getUser()->clearCredentials();
    $this->redirect('@login');
  }

}
